package com.hbisedm.interninfosys.intern.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbisedm.interninfosys.commonutils.R;
import com.hbisedm.interninfosys.intern.entity.InternshipInfo;
import com.hbisedm.interninfosys.intern.entity.InternshipUser;
import com.hbisedm.interninfosys.intern.entity.User;
import com.hbisedm.interninfosys.intern.entity.queryVo.SampleQuery;
import com.hbisedm.interninfosys.intern.entity.queryVo.UserQuery;
import com.hbisedm.interninfosys.intern.entity.vo.UserInternshipVo;
import com.hbisedm.interninfosys.intern.service.IInternshipInfoService;
import com.hbisedm.interninfosys.intern.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Sam
 * @since 2020-12-03
 */
@RestController
@RequestMapping("/intern/internship-info")
public class InternshipInfoController {
    @Autowired
    IUserService userService;
    @Autowired
    IInternshipInfoService internshipInfoService;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    /**
     * 获取学生列表
     */
    @PostMapping("/list/{current}/{limit}")
    public R internshipList(@PathVariable long current,
                         @PathVariable long limit,
                         @RequestBody(required = false) UserQuery userQuery) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        //实习单位type 为 3
        userQueryWrapper.eq("TYPE", 3);
        String name = userQuery.getName();
        String begin = userQuery.getBegin();
        String end = userQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            userQueryWrapper.like("USERNAME", name);
        }
        if(!StringUtils.isEmpty(begin)){
            userQueryWrapper.ge("CREATE_TIME", begin);
        }
        if(!StringUtils.isEmpty(end)){
            userQueryWrapper.le("CREATE_TIME", end);
        }
        Page<User> userPage = new Page<>(current, limit);
        userService.page(userPage,userQueryWrapper);
        long total = userPage.getTotal();
        List<User> users = userPage.getRecords();

        /**
         * todo 封装实习单位 InternshipUser
         */
        List<Integer> iidList = users.stream().map(User::getId).collect(Collectors.toList());
        QueryWrapper<InternshipInfo> internshipInfoQueryWrapper = new QueryWrapper<>();
        internshipInfoQueryWrapper.in("UID", iidList);
        List<InternshipInfo> internshipInfos = internshipInfoService.list(internshipInfoQueryWrapper);

        ArrayList<InternshipUser> internshipUsers = new ArrayList<>();

        for (int i = 0; i < users.size(); i++) {
            /**
             * 获取当前用户id
             */
            Integer id = users.get(i).getId();
            /**
             * 通过id 获取 实习单位信息
             */
            List<InternshipInfo> collect = internshipInfos.stream().filter(internshipInfo -> id.equals(internshipInfo.getUid())).collect(Collectors.toList());

            InternshipInfo internshipInfo = collect.get(0);
            InternshipUser internshipUser = new InternshipUser();
            BeanUtils.copyProperties(users.get(i), internshipUser);
            BeanUtils.copyProperties(internshipInfo, internshipUser);
            internshipUsers.add(internshipUser);
        }
        return R.ok().data("internships", internshipUsers)
                .data("total",internshipUsers.size());
    }

    @GetMapping("/list")
    public R internshipSampleList() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("TYPE", 3);
        List<User> list = userService.list(userQueryWrapper);
        ArrayList<SampleQuery> results = new ArrayList<>();
        for (User user : list) {
            SampleQuery sampleQuery = new SampleQuery();
            sampleQuery.setId(user.getId());
            sampleQuery.setValue(user.getOthername());
            results.add(sampleQuery);
        }
        return R.ok().data("list", results);
    }

    @GetMapping("/getInternship/{id}")
    public R getUser(@PathVariable Integer id) {
        InternshipUser internshipUser = new InternshipUser();
        User user = userService.getById(id);
        if(user != null) {
            BeanUtils.copyProperties(user, internshipUser);
        }
        QueryWrapper<InternshipInfo> infoQueryWrapper = new QueryWrapper<>();
        infoQueryWrapper.eq("UID", id);
        InternshipInfo one = internshipInfoService.getOne(infoQueryWrapper);
        if(one != null) {
            BeanUtils.copyProperties(one, internshipUser);
        }
        //再次copy导致主键变化，出现主键不是user的主键的bug
        internshipUser.setId(id);
        return R.ok().data("internshipUser", internshipUser);
    }

    @PostMapping("/updateInternInfo")
    public R updateInternInfo(@RequestBody UserInternshipVo userInternshipVo) {

        System.out.println(userInternshipVo);
        if(userInternshipVo != null) {
            User user = userService.getById(userInternshipVo.getId());
            user.setUsername(userInternshipVo.getUsername());
            user.setOthername(userInternshipVo.getInternshipName());
            user.setPassword(bCryptPasswordEncoder.encode(userInternshipVo.getPassword()));
            user.setType(3);
            user.setEmail(userInternshipVo.getEmail());
            user.setMobilephone(userInternshipVo.getMobilephone());
            userService.updateById(user);
            InternshipInfo one = internshipInfoService.getOne(new QueryWrapper<InternshipInfo>().lambda().eq(InternshipInfo::getUid, userInternshipVo.getId()));
            one.setLocationInfo(userInternshipVo.getLocationInfo());
            one.setInternshipIntroduce(userInternshipVo.getInternshipIntroduce());
            one.setInternshipName(userInternshipVo.getInternshipName());
            internshipInfoService.updateById(one);
        }
        return R.ok();
    }

}
